package backend.service.admin;

import backend.model.admin.Team;
import backend.service.base.DBModelServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class TeamServiceImpl extends DBModelServiceImpl<Team> implements TeamService {

    @Override
    public Class<Team> getEntityClass() {
        return Team.class;
    }

    @Override
    public List<Team> findAllTeams() {
        return dbModelDao.findByNamedQuery("findAllTeams");
    }

    @Override
    public List<Team> findAllActiveTeams() {
        return dbModelDao.findByNamedQuery("findAllActiveTeams");
    }

    @Override
    public List<Team> findAllActiveTeamsOfForeman(int foremanId) {
        List<Team> teams = dbModelDao.findByNamedQueryAndParams("findAllActiveTeamsOfForeman",
                new String[]{"foremanId"}, new Object[]{foremanId});
        if (teams == null) {
            return Collections.emptyList();
        }
        return teams;
    }

    @Override
    public List<Team> findAllTeamsOfForeman(int foremanId) {
        List<Team> teams = dbModelDao.findByNamedQueryAndParams("findAllTeamsOfForeman",
                new String[]{"foremanId"}, new Object[]{foremanId});
        if (teams == null) {
            return Collections.emptyList();
        }
        return teams;
    }

    @Override
    public Team findTeamByName(String name) {
        List<Team> teamsByName = dbModelDao.findByNamedQueryAndParams("findTeamByName", new String[]{"name"}, new Object[]{name});
        if (teamsByName.size() == 1) {
            return teamsByName.get(0);
        }
        return null;
    }

    @Override
    public Team findDefaultTeam() {
        List<Team> teamsByName = dbModelDao.findByNamedQuery("findDefaultTeam");
        if (teamsByName.size() == 1) {
            return teamsByName.get(0);
        }
        return null;
    }
}
